from lxml import etree
import pymssql
import json
import requests
def dbHandle():
    conn = pymssql.connect(
        host='localhost',
        user='sa',
        password='123456',
        charset='utf8',
        database='Test'
    )
    return conn

class ZhihuxjjPipeline(object):
    def process_item(self):
        dbObject = dbHandle()  # 写入数据库
        cursor = dbObject.cursor()
        #sql = "insert into xiaojiejie_zhihu([user_name],[sex],[user_sign],[user_avatar],[user_url],[var_add]) values(%s,%s,%s,%s,%s,%s)"
        #param = ('xjj','jfffj','xjj','xasdasjj','xjj','xjj')
        #sql = "insert into xiaojiejie_zhihu([user_name],[sex],[user_sign],[user_avatar],[user_url],[var_add]) values('xjj','jj','xjj','xjj','xjj','xsdfsdjj')"
        sql = 'select user_name,user_avatar from xiaojiejie_zhihu'
        try:
            cursor.execute(sql)
            for (user_name,user_avatar) in cursor.fetchall():
                print("ID:"+str(user_name)+",Title:"+user_avatar)
        except Exception as e:
            print(e)
            dbObject.rollback()
        #return item

#html = requests.get('https://www.cnblogs.com/').content
#xml = etree.HTML(html)
#//*[@id="post_list"]/div[1]/div[2]/p/text()
#//*[@id="post_list"]/div[1]/div[2]/h3/a

#f = open('text2.txt','wb')
#f.write(res)
#f.close()
#print('完成')



html = requests.get('http://www.baidu.com/s?wd=python入门')
html.encoding = 'utf-8'
f = open('baidu.txt','w',encoding='UTF-8')
f.write(html.text)
f.close()
print(html.text)



